package com.haiyisoft.eclipse.uep.generate.simple;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.osgi.framework.BundleContext;
import org.osgi.service.prefs.BackingStoreException;

import com.haiyisoft.eclipse.common.CommonPlugin;

/**
 * The activator class controls the plug-in life cycle
 */
public class SimplePlugin extends CommonPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.haiyisoft.eclipse.uep.generate.simple2"; //$NON-NLS-1$

	// The shared instance
	private static SimplePlugin plugin;
	
	public static final String VALIDATE_TAB1 = "icons/validatetab1.gif";
	public static final String VALIDATE_TAB2 = "icons/validatetab2.gif";
	public static final String JSP_PREVIEW_TAB = "icons/file_obj.gif";
	public static final String JAVA_PREVIEW_TAB = "icons/jcu_obj.gif";
	
	public static final String FOLDER_GIF = "icons/folderOpen.gif";
	
	public static final String SET_GRID = "icons/gridset.gif";
	public static final String SET_JSP = "icons/jspset.gif";
	
	public static final String ADD_GIF = "icons/toolItem/add.gif";
	public static final String DEL_GIF = "icons/toolItem/remove.gif";
	public static final String UP_GIF = "icons/toolItem/up.gif";
	public static final String DOWN_GIF = "icons/toolItem/down.gif";
	
	public static final String PACKAGE_OBJ = "icons/package_obj.gif";
	
	public static final String SIMPLE_WEBPAGE_ACTION = "icons/webpage.png";
	
	/**
	 * The constructor
	 */
	public SimplePlugin() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static SimplePlugin getDefault() {
		return plugin;
	}
	
	public void log(String msg, Exception e) {
		getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, 0, msg, e));
	}

	public void log(String s) {
		getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, 0, s, null));
	}
	
	@Override
	protected void initializeImageRegistry(ImageRegistry reg) {
		super.initializeImageRegistry(reg);
		registryDescriptor(reg, VALIDATE_TAB1);
		registryDescriptor(reg, VALIDATE_TAB2);
		registryDescriptor(reg, JSP_PREVIEW_TAB);
		registryDescriptor(reg, JAVA_PREVIEW_TAB);
		registryDescriptor(reg, SET_JSP);
		registryDescriptor(reg, SET_GRID);
		registryDescriptor(reg, ADD_GIF);
		registryDescriptor(reg, DEL_GIF);
		registryDescriptor(reg, UP_GIF);
		registryDescriptor(reg, DOWN_GIF);
		registryDescriptor(reg, PACKAGE_OBJ);
		registryDescriptor(reg, SIMPLE_WEBPAGE_ACTION);
		registryDescriptor(reg, FOLDER_GIF);
	}

	private void registryDescriptor(ImageRegistry reg, String path) {
		ImageDescriptor imageDesc = getImageDescriptor(PLUGIN_ID, path);
		reg.put(path, imageDesc);
	}
	
	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
	
	public static IEclipsePreferences getProjectSettings(IProject project) {
		ProjectScope projectscope = new ProjectScope(project);
		IEclipsePreferences preference = projectscope
				.getNode(PLUGIN_ID);
		return preference;
	}

	public static void flushProjectSettings(IEclipsePreferences preference) {
		try {
			preference.flush();
		} catch (BackingStoreException e) {
			SimplePlugin.getDefault().log(PLUGIN_ID,
					"Failed to save preferences change", e);
		}
	}
}
